@import("js.git");

const determineFilesToCopy = function (vars, cwd, env) {

    let { project , ui , goblin } = vars;

    let gobalRule = {
        "!valueType" : "object",
        "!defaultValue" : {},
        kitties : {
            "!valueType" : "object",
            "!defaultValue" : {}
        }
    }

    let config = @.fs.readFile.sync(@path(cwd,"conf",`mewchan.json`),"json");

    let hakoJSON = {};

    if (@.fs.exists.file(@path(cwd,"conf",`hako.${project}.json`))){
        hakoJSON = @.fs.readFile.sync(@path(cwd,"conf",`hako.${project}.json`),"json");
    } else if (@.fs.exists.file(@path(cwd,"conf",`hako.json`))) {
        hakoJSON = @.fs.readFile.sync(@path(cwd,"conf",`hako.json`),"json");
    }

    config = @.merge.advanced(gobalRule,config,hakoJSON);

    if (goblin) {

        hakoJSON = @.merge.advanced(gobalRule,hakoJSON,{
            kitties : {
                goblin : {
                    trackers : [goblin]
                }
            }
        })
        
    }

    let kitties = @.keys(config.kitties).reduce(function (kitties, kitty) {

        let kittyName = kitty.split(".")[0];
        
        if (kitties.indexOf(kittyName) < 0) {
            kitties.push(kittyName);
        };

        return kitties;
    },[]);

    @.fs.makeDirs(@path(cwd,"dist",project ? project : "slime","kitties"));
    @.fs.makeDirs(@path(cwd,"dist",project ? project : "slime","conf"));

    return @.async.all(kitties,function (kitty) {

        @.fs.copyFile(@path(cwd,"kitties",kitty), @path(cwd,"dist",project ? project : "slime","kitties",kitty)).pipe(this);

    }).then(function () {

        @.fs.copyFile(@path(cwd,"conf","mewchan.json"),@path(cwd,"dist",project ? project : "slime","conf","mewchan.json")).pipe(this);

    }).then(function () {
        @.fs.writeFile(@path(cwd,"dist",project ? project : "slime","conf","hako.json"),JSON.stringify(hakoJSON,null,4)).pipe(this);
    }).then(function () {
        @git.status(cwd).pipe(this);
    }).then(function (status) {
        @.fs.writeFile(@path(cwd,"dist",project ? project : "slime","version.txt"),@.format.date(status.lastCommitDate, "YYYY-MM-DD") + "-" + status.headName + "-" + @.hash.md5(status.headName + status.lastCommitDate).substr(0,8)).pipe(this);
    })
}

module.exports={
    "vars": {
        "ui": {
            "!valueType": "boolean",
            "!defaultValue": true
        },
        "project": {
            "!valueType": "string"
        },
        "goblin": {
            "!valueType": "string"
        },
    },
    "deps": {
        "goblin" : "http://develop.coolcto.com/other/goblin-project.git"
    },
    "build": [
        ["rm","-rf","dist/"],
        ["hako", "kitty", "for-each", "${project ? '--usage=' + project : '' }","-","npm","install"],
        determineFilesToCopy,
        ["cd","${getDepPath('goblin')}"],
        ["mew_bot","client"],
        ["cd","${getDepPath()}"],
        ["cp","-fr","${getDepPath('goblin') + '/dist/goblin'}","${'dist/' + (project ? project : 'slime') + '/kitties/goblin'}"],
    ],
    "dists": {
        
    }
}